var ns, app, dialog, toast;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        IsProject: false,
        ProjectID: 0,
        ProjectName: '',
        ProjectList: [],
        // 顶部日期选择
        dayModel: {},
        dayList: [],
        SelectTime: '',
        IsOpenAll: false,
        SelectIsOpen: {},
    },
    methods: {
        //选择项目
        dochooseproject: function () {
            var that = this;
            that.IsProject = !that.IsProject;
        },
        onChooseProject: function (item) {
            var that = this;
            that.ProjectID = item.ID;
            that.ProjectName = item.Name;
            that.IsProject = false;
            that.get_data();
        },
        getProjectList() {
            var that = this;
            var options = {
                action: 'getmytopprojectlist'
            };
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.ProjectList = data.list || [];
                    if (that.ProjectList.length > 0) {
                        that.onChooseProject(that.ProjectList[0]);
                    }
                }
            });
        },
        //使用情况
        getDate: function () {
            var that = this;
            var newdayList = [];
            var LenTime = 7;
            for (var i = 0; i < LenTime; i++) {
                var nowDate = moment().add(i, 'days').format("YYYY-MM-DD");
                const weeknumber = moment(nowDate).day();//获取阿拉伯数字星期
                let weeks = weeknumber == 0 ? 7 : weeknumber;
                var dayDate = { ID: i + 1, Title: '今天', Week: '', Date: nowDate, MomentDate: '', WeekNumber: weeks };
                dayDate.MomentDate = moment(nowDate).format("MM月DD日");
                dayDate.Week = that.getweekday(nowDate);
                dayDate.Title = '周' + dayDate.Week;
                if (i == 0) {
                    dayDate.Title = '今日';
                }
                if (i == 1) {
                    dayDate.Title = '明日';
                }
                newdayList.push(dayDate);
            }

            that.dayList = newdayList;
            that.onDayChoose(that.dayList[0]);//初始化
        },
        getweekday: function (date) {
            var weekArray = new Array("日", "一", "二", "三", "四", "五", "六");
            var week = weekArray[new Date(date).getDay()];//注意此处必须是先new一个Date
            return week;
        },
        //选择时间
        onDayChoose(item) {
            var that = this;
            that.dayModel = item;
            that.SelectTime = that.dayModel.Date;
            that.get_data();
        },
        tel_phone: function (phone) {
            api.call({
                type: 'tel_prompt',
                number: phone
            });
        },
        get_data: function () {
            var that = this;
            if (!that.ProjectID) {
                return;
            }
            var options = {
                action: 'getmeetingroomlist',
                AppointmentDate: that.SelectTime,
                ProjectID: that.ProjectID
            };
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.list = data.MeetingRoomList;
                    that.onDealTime();//使用情况显示处理
                }
                else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
            }, {
                toast: true
            });
        },
        onDealTime() {
            var that = this;
            var data = that.list;
            if (data.length > 0) {
                data.forEach((item, index, array) => {
                    var nowDate = moment(that.SelectTime).format("YYYY/MM/DD");
                    var time1 = new Date(nowDate + ' ' + item.OpenStarTime);
                    var time2 = new Date(nowDate + ' ' + item.OpenEndTime);
                    var diffhours = moment(time2).diff(moment(time1), 'minutes');
                    var JGHours = 15;//相差15分钟
                    var numberdiff = Math.ceil(diffhours / JGHours)
                    var list = [];
                    for (var i = 0; i < numberdiff; i++) {
                        var model = { ID: i + 1, time: '', timetitle: '', endtime: '', endtimetitle: '', status: -1, tiptime: '', tipendtime: '', nowDate: nowDate, showtime: false, IsOpen: false };
                        let startOfDay = time1;
                        model.time = moment(startOfDay).add(i * JGHours, 'minutes').format("YYYY/MM/DD HH:mm:ss");
                        model.endtime = moment(startOfDay).add((i + 1) * JGHours, 'minutes').format("YYYY/MM/DD HH:mm:ss");
                        model.timetitle = moment(model.time).format("HH:mm");
                        model.endtimetitle = moment(model.endtime).format("HH:mm");
                        if (i == (numberdiff - 1)) {
                            model.endtime = moment(time2).format("YYYY/MM/DD HH:mm:ss");
                            model.endtimetitle = item.OpenEndTime;
                        }
                        if (item.ReservationList.length > 0) {
                            var returnStatus = that.onReturnSattus(item.ReservationList, model.time, model.endtime, model);
                            model.status = returnStatus;
                        }
                        //显示间隔两个小时
                        var diffshow = moment(time1).diff(moment(model.time), 'minutes');
                        if (diffshow % 120 == 0) {
                            model.showtime = true;
                        }
                        list.push(model);
                    }
                    item.list = list;
                });
            }
        },
        onReturnSattus(data, starttime, endtime, model) {
            var returnStatus = -1;
            var starttimes = moment(starttime);
            var endtimes = moment(endtime);
            data.forEach((item, index, array) => {
                if (item.Status < 2) {
                    var diffdate = moment(item.ReservationDate).format("YYYY/MM/DD");
                    var difftime1 = new Date(diffdate + ' ' + item.ReservationStartTime);
                    var difftime2 = new Date(diffdate + ' ' + item.ReservationEndTime);
                    var date1 = moment(difftime1);
                    var date2 = moment(difftime2);
                    const isBetween = starttimes.isBetween(date1, date2, null, '[)');
                    const isBetween2 = endtimes.isBetween(date1, date2, null, '(]')
                    if (isBetween || isBetween2) {
                        returnStatus = item.Status;
                        model.tiptime = item.ReservationStartTime;
                        model.tipendtime = item.ReservationEndTime;
                    }
                }
            })
            return returnStatus;
        },
        openAdd: function (item) {
            var that = this;
            ns.openWin('visitormeetreserveadd', '会议室预约', {
                ID: item.ID,
                ProjectID: that.ProjectID,
            });
        },
        onOpenShowTime: function (list, itemt) {
            var that = this;
            itemt.IsOpen = true;
            that.IsOpenAll = !that.IsOpenAll;
            that.SelectIsOpen = itemt;
            list.forEach((ite) => {
                if (ite.ID != itemt.ID) {
                    ite.IsOpen = false;
                }
            });
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    dialog = new auiDialog();
    toast = new auiToast();

    app.ID = api.pageParam.ID || 0;
    app.getProjectList();
    app.getDate();


    api.addEventListener({
        name: 'reloadmeetservicelist'
    }, function (ret, err) {
        app.get_data();
    });
};
